package com.app.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.app.common.Result;
import com.app.model.*;
import com.app.service.*;
import com.app.utils.TokenUtils;
import com.app.vo.LotteryRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 抽奖记录 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-16
 */
@RestController
@RequestMapping("/lotteryRecord")
public class LotteryRecordController {
    @Autowired
    private ILotteryRecordService iLotteryRecordService;
    @Autowired
    private UserService iUserService;
    @Autowired
    private InviteConfigService inviteConfigService;
    @Autowired
    private AddressService addressService;
    /**
     * 我的中奖记录
     *
     * @param activityId 活动id
     * @return {@link Result}
     */
    @GetMapping("/myLotteryRecord")
    public Result myLotteryRecord(Integer page ,Integer limit,Integer activityId, HttpServletRequest request, Integer type) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = iUserService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(201, "用户不存在");
        }
        Integer userId = userModel.getId();
        return Result.success(iLotteryRecordService.myLotteryRecord(userId, activityId,type,page,limit));
    }


    /**
     * 彩票记录
     *
     * @return {@link Result}
     */
    @GetMapping("/admin/lotteryRecord")
    public Result lotteryRecord(Integer page, Integer limit, Integer isSend, Integer type) {
        Page page1 = new Page(page, limit);
        QueryWrapper<LotteryRecord> lotteryRecordQueryWrapper = new QueryWrapper<>();
        lotteryRecordQueryWrapper.eq("is_hit", 1);
        if (ObjectUtil.isNotEmpty(isSend)) {
            lotteryRecordQueryWrapper.eq("is_send", isSend);
        }
        if (ObjectUtil.isNotEmpty(type)) {
            lotteryRecordQueryWrapper.eq("type", type);
        }
        lotteryRecordQueryWrapper.orderByDesc("draw_time");
        IPage<LotteryRecord> recordIPage = iLotteryRecordService.page(page1, lotteryRecordQueryWrapper);
        for (LotteryRecord lotteryRecord : recordIPage.getRecords()) {
            UserModel user = iUserService.getById(lotteryRecord.getUserId());
            lotteryRecord.setUser(user);
        }
        LotteryRecordVo vo = new LotteryRecordVo();
        vo.setCount((int) recordIPage.getTotal());
        vo.setList(recordIPage.getRecords());
        return Result.success(vo);
    }


    @GetMapping("/admin/recordDetail")
    public Result recordDetail(@RequestParam(value = "id") Long id) {
        LotteryRecord record = this.iLotteryRecordService.getById(id);
        if (record!=null){
            QueryWrapper<AddressModel> addressModelQueryWrapper = new QueryWrapper<>();
            addressModelQueryWrapper.eq("user_id",record.getUserId());
            addressModelQueryWrapper.eq("is_default",1);
            AddressModel addressModel = addressService.getOne(addressModelQueryWrapper);
            if (addressModel!=null){
                record.setAddressId(addressModel.getId());
                iLotteryRecordService.updateById(record);
                record.setAddressModel(addressModel);
            }
            UserModel userModel = iUserService.getById(record.getUserId());
            if (userModel!=null){
                record.setUser(userModel);
            }
//            CategoryModel categoryModel = categoryService.getById(record.getExpressCompany());
//            if (categoryModel != null) {
//                record.setExpressCompanyText(categoryModel.getName());
//            }
            if (record.getMessage() != null) {
                Object parse = JSON.parse(record.getMessage());
                record.setDeliveryVo(parse);
            }
            record.setMessage(null);
        }
        return Result.success(record);
    }

    /**
     * 发送
     *
     * @param lotteryRecord 彩票记录
     * @return {@link Result}
     */
    @PostMapping("/admin/send")
    public Result send(@RequestBody LotteryRecord lotteryRecord) {
        if (lotteryRecord.getId() != null) {
            lotteryRecord.setIsSend(3);
            iLotteryRecordService.updateById(lotteryRecord);
        }
        return Result.success("");
    }

    @GetMapping("/confirmReceipt")
    public Result confirmReceipt(String orderSn) {
        QueryWrapper<LotteryRecord> lotteryRecordQueryWrapper = new QueryWrapper<>();
        lotteryRecordQueryWrapper.eq("order_sn",orderSn);
        LotteryRecord one = iLotteryRecordService.getOne(lotteryRecordQueryWrapper);
        if (one != null) {
            one.setIsSend(2);
            iLotteryRecordService.updateById(one);
            return Result.success("收货成功");
        }else {
            return Result.error(201,"订单不存在");
        }
    }


    /**
     * 收到货物
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/receivedGoods")
    public Result receivedGoods(@RequestParam(value = "id") Integer id) {
        LotteryRecord record=this.iLotteryRecordService.getById(id);
        if(record.getType()==1&&record.getIsSend()==4)
        {
            return Result.error(201,"奖品已失效");
        }
        record.setIsSend(2);
        this.iLotteryRecordService.updateById(record);
        return Result.success(null);
    }


}

